package templateExecel2.extract;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtractSheet {

    int mode = 1;

    public ExtractSheet colEquals() {
        mode = 1;
        return this;
    }

    public ExtractSheet colIntersection() {
        mode = 2;
        return this;
    }


    public ExtractSheet colTemplateFull() {
        mode = 3;
        return this;
    }


    List<Row> rows = new ArrayList<>();

    class Row {
        Map<Integer, Cell> cellMap = new HashMap<>();

        class Cell {
            Object v;
            int cell;
            int row;
            int col;

            ExtractSheet sheet() {
                return ExtractSheet.this;
            }
        }

        ExtractSheet sheet() {
            return ExtractSheet.this;
        }

        public Row cell(String cell, Object v) {
            return cell(cell, v, 1, 1);
        }


        public Row cell(String cell, Object v, int row, int col) {
            cell = cell.toUpperCase();
            char[] chars = cell.toCharArray();
            int sum = 0;
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] < 'A' || chars[i] > 'Z') {
                    throw new RuntimeException("无效标记:" + chars[i]);
                }
                sum += (chars[i] - 'A') * Math.pow(26, i);
            }
            return cell(sum, v, row, col);
        }

        public Row cell(int cell, Object v) {
            return cell(cell, v);
        }

        public Row cell(int cell, Object v, int row, int col) {

            Cell ce = new Cell();
            ce.cell = cell;
            ce.v = v;
            if (cellMap.containsKey(cell)) {
                throw new RuntimeException("无法添加重复的单元格:" + cell);
            }
            ce.row=row;
            ce.col = col;
            cellMap.put(cell, ce);
            return this;
        }


    }

    public Row newRow() {
        Row row = new Row();
        rows.add(row);
        return row;
    }

}
